﻿/*
 *FileName:      LoopScrollEditor.cs
 *Author:        ChangYou
 *Date:          2023/09/12 09:24:50
 *UnityVersion:  2021.3.15f1c1
 *Description:   无限滑动  Inspector 界面 编辑 
*/
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(LoopScroll), true)]
public class LoopScrollEditor : Editor
{
    private LoopScroll curScript;
    private SerializedProperty scroll,scrollAxis, content, itemPrefab, itemSize, spacingV2, spacing, paddingTop, paddingBottom, paddingLeft, paddingRight,
                               activeCountCol, activeCountRow, isAutoLayout;

    private RectTransform contentRect;
    private RectTransform scrollRect;
    private bool isFoldPadding = false;

    public void OnEnable()
    {
        curScript = (LoopScroll)serializedObject.targetObject;
        scrollAxis = serializedObject.FindProperty("scrollAxis");
        scroll = serializedObject.FindProperty("scroll");
        content = serializedObject.FindProperty("content");
        itemPrefab = serializedObject.FindProperty("itemPrefab");
        itemSize = serializedObject.FindProperty("itemSize");
        spacing = serializedObject.FindProperty("spacing");
        spacingV2 = serializedObject.FindProperty("spacingV2");
        paddingTop = serializedObject.FindProperty("paddingTop");
        paddingBottom = serializedObject.FindProperty("paddingBottom");
        paddingLeft = serializedObject.FindProperty("paddingLeft");
        paddingRight = serializedObject.FindProperty("paddingRight");
        activeCountCol = serializedObject.FindProperty("activeCountCol");
        activeCountRow = serializedObject.FindProperty("activeCountRow");
        isAutoLayout = serializedObject.FindProperty("isAutoLayout");
    }
    public override void OnInspectorGUI()
    {
        if (Application.isPlaying) return;
        serializedObject.Update();
        EditorGUILayout.PropertyField(scroll, new GUIContent("Scroll"));
        EditorGUILayout.PropertyField(scrollAxis, new GUIContent("ScrollAxis"));
        EditorGUILayout.PropertyField(content, new GUIContent("Content"));
        EditorGUILayout.PropertyField(itemPrefab, new GUIContent("ItemPrefab"));
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PropertyField(itemSize, new GUIContent("ItemSize"));
        if (GUILayout.Button("获取", new[] { GUILayout.Height(20), GUILayout.Width(100) }))
        {
            curScript.GetItemSize();
        }
        EditorGUILayout.EndHorizontal();
        var tempScroller = curScript.transform.GetComponent<UnityEngine.UI.ScrollRect>();
        if (tempScroller == null)
        {
            tempScroller = curScript.gameObject.AddComponent<UnityEngine.UI.ScrollRect>();
        }
        if (curScript.scroll == null)
        {
            curScript.scroll = tempScroller;
        }
        switch (curScript.scrollAxis)
        {
            case LoopScroll.LoopScrollType.Horizontal:
                if (curScript.content != null)
                {
                    tempScroller.content = contentRect;
                    contentRect = curScript.content.GetComponent<RectTransform>();
                    scrollRect = curScript.scroll.GetComponent<RectTransform>();
                    scrollRect.pivot = new Vector2(0, 0.5f);

                    //contentRect.anchorMin = new Vector2(0,0.5f);
                    //contentRect.anchorMax = new Vector2(0, 0.5f);
                    //contentRect.pivot = new Vector2(0, 0.5f);
                    //contentRect.sizeDelta = scrollRect.sizeDelta;
                    contentRect.anchoredPosition = Vector2.zero;
                }
                tempScroller.horizontal = true;
                tempScroller.vertical = false;
                EditorGUILayout.PropertyField(activeCountCol, new GUIContent("ActiveCountCol(活动列)"));
                EditorGUILayout.PropertyField(spacing, new GUIContent("Spacing"));
                isFoldPadding = EditorGUILayout.Foldout(isFoldPadding, "Padding");
                if (isFoldPadding)
                {
                    EditorGUI.indentLevel++;
                    EditorGUILayout.PropertyField(paddingLeft, new GUIContent("Left"));
                    EditorGUILayout.PropertyField(paddingRight, new GUIContent("Right"));
                    EditorGUI.indentLevel--;
                }
                break;
            case LoopScroll.LoopScrollType.Vertical:
                if (curScript.content != null)
                {
                    tempScroller.content = contentRect;
                    contentRect = curScript.content.GetComponent<RectTransform>();
                    scrollRect = curScript.scroll.GetComponent<RectTransform>();
                    scrollRect.pivot = new Vector2(0.5f, 1f);

                    //contentRect.anchorMin = new Vector2(0.5f, 1);
                    //contentRect.anchorMax = new Vector2(0.5f, 1);
                    //contentRect.pivot = new Vector2(0.5f, 1f);
                    //contentRect.sizeDelta = scrollRect.sizeDelta;
                    contentRect.anchoredPosition = Vector2.zero;
                }
                tempScroller.horizontal = false;
                tempScroller.vertical = true;
                EditorGUILayout.PropertyField(isAutoLayout, new GUIContent("IsAutoLayout(是否自动排版)"));
                if (!curScript.isAutoLayout)
                {
                    EditorGUI.indentLevel++;
                    EditorGUILayout.PropertyField(activeCountRow, new GUIContent("ActiveCountRow(活动行)"));
                    EditorGUILayout.PropertyField(activeCountCol, new GUIContent("ActiveCountCol(活动列)"));
                    EditorGUILayout.PropertyField(spacingV2, new GUIContent("SpacingV2"));
                    isFoldPadding = EditorGUILayout.Foldout(isFoldPadding, "Padding");
                    if (isFoldPadding)
                    {
                        EditorGUI.indentLevel++;
                        EditorGUILayout.PropertyField(paddingTop, new GUIContent("Top"));
                        EditorGUILayout.PropertyField(paddingBottom, new GUIContent("Bottom"));
                        EditorGUILayout.PropertyField(paddingLeft, new GUIContent("Left"));
                        EditorGUILayout.PropertyField(paddingRight, new GUIContent("Right"));
                        EditorGUI.indentLevel--;
                    }
                    EditorGUI.indentLevel--;
                }
                else
                {
                    EditorGUI.indentLevel++;
                    isFoldPadding = EditorGUILayout.Foldout(isFoldPadding, "Padding");
                    if (isFoldPadding)
                    {
                        EditorGUI.indentLevel++;
                        EditorGUILayout.PropertyField(paddingTop, new GUIContent("Top"));
                        EditorGUILayout.PropertyField(paddingBottom, new GUIContent("Bottom"));
                        EditorGUILayout.PropertyField(paddingLeft, new GUIContent("Left"));
                        EditorGUILayout.PropertyField(paddingRight, new GUIContent("Right"));
                        EditorGUI.indentLevel--;
                    }
                    EditorGUI.indentLevel--;
                }
                break;
            default:
                break;
        }
        
        EditorGUILayout.BeginHorizontal();
        if (GUILayout.Button("预览排版", new[] { GUILayout.Height(20), GUILayout.Width(120) }))
        {
            curScript.Preview();
        }
        if (GUILayout.Button("取消预览", new[] { GUILayout.Height(20), GUILayout.Width(120) }))
        {
            curScript.CancelPreview();
        }
        EditorGUILayout.EndHorizontal();
        serializedObject.ApplyModifiedProperties();
        this.Repaint();
    }
}
